<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="960px" :before-close="handleCancel">
    <div class="">
      <div class="page-filter mb10 fl">
        <el-form ref="filter" label-width="0">
          <el-form-item class="mr10 mb5 fl" style="width: 300px">
            <el-button class="fl" type="primary" size="small" @click="handleAdd">新 增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        ref="multipleTable"
        class="search-table"
        :data="list"
        :summary-method="getSummaries"
        show-summary
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="序号" align="center" width="56px">
          <template slot-scope="{$index}">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="付款方式" min-width="260px" prop="value" />
        <el-table-column label="付款比例" align="right" width="140px" :formatter="proportion" />
        <el-table-column
          :label="'付款金额' + '（' + app.toFix(this.form.trading_price, 2) + ')'"
          align="right"
          width="260px"
          prop="money"
        >
          <template slot-scope="{row}">
            {{ app.toFix(row.money, 2) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="110px" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown style="height:29px">
              <el-button size="mini" type="primary">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    </div>
  </el-dialog>
</template>

<script>
import { proPays, delPays } from '@/api/project'
import Dialog from './paytype-dialog'

export default {
  components: { Dialog },
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      total: 0,
      list: [],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'form', this.info.edit)
        this.getList()
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout()
        })
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList(e) {
      this.app.loading = true
      proPays({
        ...this.filter,
        ...e,
        ...{ 'pro_id': this.form.pro_id }
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    proportion(e) {
      if (this.form.trading_price === 0) return '100.00%'
      return this.app.toFix((e.money / this.form.trading_price) * 100, 2) + '%'
    },
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = this.app.toFix(sums[index], 2)
        } else {
          sums[index] = ''
          if (index === 3) {
            sums[index] = this.app.toFix((sums[index - 1] / this.form.trading_price) * 100, 2) + '%'
          }
        }
      })

      return sums
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delPays({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleEdit(e) {
      this.dialog.title = '编辑付款方式'
      this.dialog.type = 'edit'
      e.pro_id = this.form.pro_id
      this.app.dialogParam(this.dialog.edit, ['id', 'pro_id', 'pay_type', 'money'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增付款方式'
      this.dialog.type = 'add'
      e.pro_id = this.form.pro_id
      this.app.dialogParam(this.dialog.edit, ['id', 'pro_id', 'pay_type', 'money'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
</style>
